#include "handleLine.h"

handleLine::handleLine(void):_imageWidth(0),_imageHeight(0),
	_handle(NULL),doing(false){
	if(!_filenameList.isEmpty()) _filenameList.empty();
}

void handleLine::initialize(){
}

void handleLine::setInputPath(const QString filename){
	QDir dir(filename);
	dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
	dir.setSorting(QDir::Size | QDir::Reversed);
	
	_filenameList = dir.entryInfoList();
}

void handleLine::setOutPutPath(const QString filepath){
	_outputPath = filepath;
}

QFileInfo handleLine::getFileNameList(int i){
	if(i<0||i>_filenameList.size())
		return NULL;
	return _filenameList[i];
} 

DWORD WINAPI DrawMarginStream(PVOID param){

	handleLine* hdline = (handleLine*)param;
	DrawMarginApp* app = hdline->getDrawMarginApp();
	DrawAttribute* drawAttribute = new DrawAttribute;

	for(int i=0; i<hdline->getFileNameSize(); i++){
		//Loading image and size!
		QFileInfo finfo = hdline->getFileNameList(i);

		if(  (finfo.suffix()!="png") &&  (finfo.suffix()!="jpg") )	continue;

		IplImage* rawImg = hdline->loadImage(finfo.absoluteFilePath());
		
		drawAttribute->setRawImage(rawImg);
		drawAttribute->extractAttributeAlgorithm();
		QString filename = hdline->getOutputPath();
		filename.append("\\");
		filename.append(finfo.fileName().left(finfo.fileName().length()-4));
		filename.append(".txt");
		QByteArray barray = filename.toLatin1();
		char *pfname = barray.data();
		drawAttribute->saveMarginalPoints(pfname);
		//show raw image
		hdline->getDrawMarginApp()->getImageView(0)->updateImage(rawImg);
		IplImage *marImg = cvCreateImage(cvGetSize(drawAttribute->getMarImage()),\
			drawAttribute->getMarImage()->depth,drawAttribute->getMarImage()->nChannels);
		marImg = cvCloneImage(drawAttribute->getMarImage());

		QString marfilename = filename.left(filename.length()-4);
		marfilename.append(".jpg");
		QByteArray bmar = marfilename.toLatin1();
		char *marfile = bmar.data();
		cvSaveImage(marfile,marImg);
		//show marginal image

		Point2i eye = drawAttribute->getEyePoint();
		Point2i cross = drawAttribute->getCrossPoint();
		Point2i head = drawAttribute->getHeadPoint();
		Point2i tail = drawAttribute->getTailPoint();
		Point2i back = drawAttribute->getBackPoint();
		Point2i belly = drawAttribute->getBellyPoint();

		cvCircle(rawImg,eye,4,CV_RGB(255,255,255),2);
		cvCircle(rawImg,cross,4,CV_RGB(255,0,0),2);
		cvCircle(rawImg,head,4,CV_RGB(0,255,0),2);
		cvCircle(rawImg,tail,4,CV_RGB(0,0,255),2);
		cvCircle(rawImg,belly,4,CV_RGB(255,255,0),2);
		cvCircle(rawImg,back,4,CV_RGB(255,255,0),2);
		cvLine(rawImg,eye,cross,CV_RGB(0,0,0),2);

		hdline->getDrawMarginApp()->getImageView(1)->updateImage(rawImg);
		hdline->getDrawMarginApp()->setFishLength((float)drawAttribute->getLength() / (float)rawImg->width );
		hdline->getDrawMarginApp()->setFishWidth( (float)drawAttribute->getWidth() / (float)rawImg->height);
		hdline->getDrawMarginApp()->setCrossLength((float)drawAttribute->getCrossLength() / (float)rawImg->width);
		hdline->getDrawMarginApp()->setMidLineLength((float)drawAttribute->getEyeCrossLength() / (float)rawImg->width);

		QString attrfilename = filename.left(filename.length()-4);
		attrfilename.append("_attr.jpg");
		QByteArray battr = attrfilename.toLatin1();
		char* attrfile = battr.data();
		cvSaveImage(attrfile,rawImg);

		cvReleaseImage(&marImg);
		marImg = NULL;
		cvReleaseImage(&rawImg);
		rawImg = NULL;
		Sleep(500);
	}

	delete drawAttribute;
	return 0;
}

IplImage* handleLine::loadImage(QString filename){
	QByteArray barray = filename.toLatin1();
	char *pfname = barray.data();
	return cvLoadImage(pfname);
}

/*
 return :  error = -1
		   exist = 1
		   sucess = 0
*/
int handleLine::execute(){
	if(_handle) {
		return 1;
	}
	_handle = CreateThread(NULL,0,DrawMarginStream,this,0,&_handleId);
	if(!_handle){
		std::cout<<"create thread failed!"<<std::endl;
		return -1;
	}
	doing = true;
	return 0;
}

void handleLine::exit(){
	//WaitForSingleObject(_handle,INFINITE);
	CloseHandle(_handle);
	_handle = NULL;
	_handleId = 0;
}

handleLine::~handleLine(void)
{
	exit();
}
